package com.cj.bus.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Code generator.
 * 代码生成工具
 *
 * @author yangh
 * @date 2018 -12-26 15:20:35
 */
public class CodeGenerator {

    /**
     * The entry point of application.
     * 代码生成main方法
     *
     * @param args the input arguments
     * @date 2018 -12-26 15:20:35
     *
     *
     * spring.datasource.url=jdbc:mysql://rds5f3gzemdw53bpsnowl.mysql.rds.aliyuncs.com:3306/erp_base?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true&serverTimezone=GMT%2B8
     * spring.datasource.username=vk_erp
     * spring.datasource.password=TE&b$GLKJT
     */
    public  static  String  projectPath="/Users/yanghao0518/soft/RuoYi/bus-front";
    public  static  String  outPutSuffixPath="/src/main/java";
    public  static  String  author="yangh73";
    public static  String dataBase = "ry";
    public  static  String  dbUrl="jdbc:mysql://127.0.0.1:3306/"+dataBase+"?useUnicode=true&characterEncoding=utf-8&useSSL=false";
    public  static  String  driverName="com.mysql.jdbc.Driver";
    public  static  String  userName="root";
    public  static  String password="yanghao0518";
    public  static  String parentPackage="com.cj.bus";
    //public  static  String incloudTables = "assets_duty_image,assets_house_image,assets_house_video,assets_property_house,assets_property_parking,assets_resource,assets_resource_contact,assets_resource_follow,assets_resource_key,assets_resource_seal,assets_resource_trust,assets_resource_watch_image,base_community,base_community_block,base_community_image,base_community_unit,base_department,base_district,base_region,client_customer,client_phone,client_resource,client_resource_follow,client_resource_seal,client_resource_visit,client_resource_visit_image";//要生成的表名称 多个逗号隔开
   // public  static  String incloudTables = "bus_accessory,bus_dept_apply,bus_news,bus_req,bus_req_apply,bus_req_comment,bus_req_progress,bus_resource,bus_resource_apply,bus_resource_comment,bus_resource_type,bus_user_browse,bus_user_collect,bus_user_message,sys_user";
    
    public  static  String incloudTables = "bus_news,bus_req,bus_resource";
    public static Map<String,String> mappingXml = new HashMap<String, String>(){{
        put(dataBase,"base.mapper.xml.vm");
        put("wk_193","mapper.xml.vm");
    }};


    /**
     * The entry point of application.
     * 代码生成main方法
     *
     * @param args the input arguments
     * @date 2018 -12-26 15:20:35
     */
    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
//        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + outPutSuffixPath);
        gc.setFileOverride(true);
        // XML ResultMap
        gc.setBaseResultMap(false);
        // XML columList
        gc.setBaseColumnList(true);
        gc.setAuthor(author);
        gc.setOpen(false);
        gc.setSwagger2(true);
        gc.setIdType(IdType.ID_WORKER_STR);
        // 自定义文件命名，注意 %s 会自动填充表实体属性！
        gc.setEntityName("%sEntity");
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setUrl(dbUrl);
        dsc.setDriverName(driverName);
        dsc.setUsername(userName);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //pc.setEntity("model");
        pc.setParent(parentPackage);
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("poPath", "model.po");
                this.setMap(map);
            }
        };

        // 如果模板引擎是 freemarker
        // String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出

        //自定义xml输出路径
        String xmlTempPath = "/templates/"+mappingXml.get(dataBase);
        focList.add(new FileOutConfig(xmlTempPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名
                return projectPath + "/src/main/resources/mapper/"
                        + "/" + tableInfo.getEntityName()+ "Mapper" + StringPool.DOT_XML;
            }
        });


        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        // 配置自定义输出模板
//         templateConfig.setEntity();
        // templateConfig.setService();
        // 不生成单表controller和xml
        templateConfig.setController(null);
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // 此处可以修改为您的表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
        //strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        //strategy.setSuperEntityColumns("id");
        strategy.setEntityLombokModel(true);
        strategy.setEntityBuilderModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setLogicDeleteFieldName("delete_flag");
        if(StringUtils.isNotBlank(incloudTables)) {
            strategy.setInclude(incloudTables.split(","));
        }
        //strategy.setTablePrefix("t_");
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new VelocityTemplateEngine());
        mpg.execute();
    }

}